define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    require('ztree');
    require('qrcode');

    var time_out = 60;
    var time_have = time_out;
    var intervalId = 0;

    var outTradeNo = "";

    $(document).on('click', '.account-charge', function () {
        accountCharge();
    }).on('blur', '#accountName', function () {
        var $form = $("#form-user");
        if ($.trim($(this).val()) == '') {
            $form.validate("showError", "accountName", "请填写充值账号", "accountNameError");
            return;
        } else {
            $form.validate("hideError", "accountName", "accountNameError");
        }
    }).on('input propertychange', '#otherAmount', function () {
        var str = $(this).val();
        var temp = /^[0-9]*$/;
        if (!temp.test(str) || str.length > 11) {
            var newValu = str.substr(0, str.length - 1);
            $(this).val(newValu);
        }
    }).on('blur', '#otherAmount', function () {
        var amount = $('input[name="amount"]:checked').val();
        if (amount != 0) {
            return;
        }
        var $form = $("#form-user");
        var amount = $.trim($('input[name="otherAmount"]').val());
        if (amount == '') {
            $form.validate("showError", "otherAmount", "请填写充值金额", "otherAmountError");
        } else {
            $form.validate("hideError", "otherAmount", "otherAmountError");
        }
    }).on('click', 'input[name="amount"]', function () {
        var $form = $("#form-user");
        var amount = $('input[name="amount"]:checked').val();
        if (amount != 0) {
            $form.validate("hideError", "otherAmount", "otherAmountError");
        }
    }).on('click', '.refresh-qrcode', function () {
        accountCharge();
    }).on('hide', function (e) {
        window.clearInterval(intervalId);
    });

    function accountCharge() {
        var $form = $("#form-user");
        var accountName = $('#accountName').val();
        if ($.trim(accountName) == '') {
            $form.validate("showError", "accountName", "请填写充值账号", "accountNameError");
            return;
        }
        var amount = $('input[name="amount"]:checked').val();
        if (amount == 0) {
            amount = $.trim($('input[name="otherAmount"]').val());
            if (amount == '') {
                $form.validate("showError", "otherAmount", "请填写充值金额", "otherAmountError");
            } else if (amount == 0) {
                $form.validate("showError", "otherAmount", "请填写充值金额", "otherAmountError");
            }
        }
        var payType = $('input[name="payType"]:checked').val();
        var data = {
            accountName: accountName,
            amount: amount,
            payType: payType
        };
        $.post('/accountCharge/charge', data, function (o) {
            switch (o.code) {
                case 0:
                    var url = o.data.qrCodeUrl;
                    outTradeNo = o.data.outTradeNo;
                    if (payType == 1) {
                        $('.pay-type-msg').html('支付宝扫码支付');
                    } else {
                        $('.pay-type-msg').html('微信扫码支付');
                    }
                    $('.pay-amount').html(amount);
                    $('#qrCodeModal').modal('show');
                    $('#qrcode').empty();
                    var qrcode = new QRCode('qrcode');
                    qrcode.clear();
                    qrcode.makeCode(url); // 更换二维码内容
                    // var qrcodeImg = qrcode._oDrawing._elImage;
                    // $(qrcodeImg).on('load', function () {
                    //     // $('.btn-download').attr('href',this.src);
                    // });
                    setTimeOut();
                    break;
                default:
                    toastError(o.msg);
            }
        })


    }


    function queryTradeStatus() {
        var payType = $('input[name="payType"]:checked').val();
        var data = {
            outTradeNo: outTradeNo,
            payType: payType
        };
        $.post('/accountCharge/query', data, function (o) {
            if (o.code == 0) {
                var tradeStatus = o.data;
                if (tradeStatus == 'SUCCESS') {
                    window.clearInterval(intervalId);
                    $('.pay-type-msg').html('<span style="color: #00B83F">恭喜您，充值成功！</span>');
                }
            }
        });
    }

    function setTimeOut() {
        $('.time-have').show();
        $('.refresh-qrcode').hide();
        window.clearInterval(intervalId);
        time_have = time_out;
        intervalId = setInterval(function () {
            time_have = time_have - 1;
            console.log(time_have);
            if (time_have == 0) {
                $('.time-have').hide();
                $('.refresh-qrcode').show();
                window.clearInterval(intervalId);
            } else {
                $('.time-second').html(time_have + "秒");
            }
            if (time_out - time_have > 5) {
                queryTradeStatus();
            }
        }, 1000);

    }

});
